// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
//widget添加响应事件
var addButtonEventS =function (btn,seat,self,efunc) {

    btn.on(cc.Node.EventType.TOUCH_END, function(event){
        if (efunc) { efunc(btn,self,seat); }
    });

};
var DN_CMD = require("zjhCMD");

cc.Class({
    extends: cc.Component,

    properties: {
        className:"headLayer",
        coin:{
            default:null,
            type:cc.Prefab,
        },
        head:{
            default:null,
            type:cc.Prefab,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        this.node.thisJS = this;
        this._players = {} ;
        this.LoseUser = {} ;    //弃牌的玩家
        // this.user0 =  cc.find("user1",this.node);
        // this.user1 =  cc.find("user2",this.node);
        // this.user2 =  cc.find("user3",this.node);
        // this.user3 =  cc.find("user4",this.node);
        // this.user4 =  cc.find("user5",this.node);
        // this.user5 =  cc.find("user6",this.node);
        // this.initUI();

        // for(i=0;i<6;i++){
        //     console.log("user"+i+"headPos--->");
        //     dump(this["user"+i].getPosition());
        //     var list = this["user"+i].getChildren();
        //    for(var k in list){
        //      console.log("user"+i+"--->"+list[k].getName());
        //      dump(list[k].getPosition());
        //    } 
        // }
        
    },
    start: function () {
        //动态添加刷新节点
        for(var i=0;i<GameData.PlayerNum;i++){
            var config = GameConfig.headConfig[i]
            var headNode = cc.instantiate(this.head);
            this.node.addChild(headNode);
            this["user"+i] = headNode ;
            headNode.active = false ;
            headNode.scale = config.scale || 1;
            headNode.setPosition(config.pos);
            addButtonEventS(headNode,i,this,this.headClick);
            var script = headNode.getComponent('userView');
            script.updateUI(config.subPos);
        }
        this.initUI();
    },
    initUI: function(){
         console.log("接收监听");
        EventManager.addEventObeserveF(EventConfig.UserEnterDeskNtf, handler(this,
            this.updateHeadView));
        EventManager.addEventObeserveF(EventConfig.UserReadyNtf, handler(this,
            this.UserReadyNtf));
        EventManager.addEventObeserveF(EventConfig.startGame, handler(this,
            this.gameStart),"headLayer");
        EventManager.addEventObeserveF(EventConfig.clearTable, handler(this,
            this.clearTable),"headLayer");
        EventManager.addEventObeserveF(EventConfig.biPaiClick, handler(this,
            this.biPaiClick),"headLayer");
        EventManager.addEventObeserveF(EventConfig.xiaZhu, handler(this,
            this.xiaZhuNtf),"headLayer");
        EventManager.addEventObeserveF(EventConfig.qiPai, handler(this,
            this.qiPaiNtf),"headLayer");
        EventManager.addEventObeserveF(EventConfig.compareNtf, handler(this,
            this.compareNtf),"headLayer");
        EventManager.addEventObeserveF(EventConfig.roundOverCompareCard, handler(this,
            this.xiaZhuNtf),"headLayer");

        EventManager.addEventObeserveF(EventConfig.gameoverNtf, handler(this,
            this.gameoverNtf),"headLayer");
        EventManager.addEventObeserveF(EventConfig.roundOverCompareCard, handler(this,
            this.roundOverCompareCard),"headLayer");

        this.biState = {};
    },
    roundOverCompareCard:function(data){
        var self = this ;
        var info = GameData ;
        for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            var searId = GameData.getUserSeatP(user.seatid);
            self["user"+searId].active = true;
            self["user"+searId].thisJS.stopTimer();
        }
    },
    gameoverNtf:function(data){
         var info = GameData ;
         for(var k in data.result){
              var userInfo = data.result[k];
              var user = GameData.playerinfo[userInfo.uin] ;
              var searId = GameData.getUserSeatP(user.seatid);
              this["user"+searId].active = true;
              this["user"+searId].thisJS.updateUserScore(userInfo.user_money);
              this["user"+searId].thisJS.stopTimer();

        }
    },
    hideBiState:function(){
        var self = this ;
        var info = GameData ;
        for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            if(user.uin != UserInfoModel.info.uin){
                var searId = GameData.getUserSeatP(user.seatid);
                console.log("更新bi状态->"+searId);
                self["user"+searId].active = true;
                self["user"+searId].thisJS.updateBiState(false);
                self.biState[searId] = null ;
            }
        }
    },
    compareNtf:function(data){
        var info = GameData ;
        for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==data.uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                if(data.result == 0){  
                  this.LoseUser[data.uin] = 1 ;
                }
            }
            if(info.playerinfo[k].uin==data.compare_uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                if(data.result == 1){  
                  this.LoseUser[data.compare_uin] = 1 ;
                }
            }
         }
        this.xiaZhuNtf(data);
    },
    
    xiaZhuNtf:function(data){
        this.hideBiState();
        var self = this ;
        var info = GameData ;
        for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            var searId = GameData.getUserSeatP(user.seatid);
            self["user"+searId].active = true;
            self["user"+searId].thisJS.stopTimer();
        }
        if(data.next_uin && data.next_uin>0){
           var user =  GameData.getUserByUin(data.next_uin);
           var searId = GameData.getUserSeatP(user.seatid);
           self["user"+searId].active = true;
           self["user"+searId].thisJS.startTimer(10);
        }
    },
    qiPaiNtf:function(data){
        this.hideBiState();
        var self = this ;
        var info = GameData ;
        for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            var searId = GameData.getUserSeatP(user.seatid);
            self["user"+searId].active = true;
            self["user"+searId].thisJS.stopTimer();
        }
        this.LoseUser[data.uin] = 1 ;
        if(data.next_uin && data.next_uin>0){
           var user =  GameData.getUserByUin(data.next_uin);
           var searId = GameData.getUserSeatP(user.seatid);
           self["user"+searId].active = true;
           self["user"+searId].thisJS.startTimer(10);
        }
    },
    /*
    //cmd：1706 用户请求比牌
    message GoldFlowerUserCompareReq{
        optional int32 uin = 1;             //用户ID
        optional int32 desk_id=2;           //桌子ID
        optional int32 compare_uin=3;       //需要比牌玩家的uin
    }
    */
    headClick:function(view,self,seat){
       if (self.biState[seat]==1){

         GameNet.sendRequest({cmd:DN_CMD.GoldFlowerUserCompareReq,
            body:{
                uin:UserInfoModel.info.uin,
                desk_id:GameData.deskid,
                compare_uin:GameData.getUinByTSeat(seat),
            }});
         console.log("发送比牌");
         var info = GameData ;
         for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            if(user.uin != UserInfoModel.info.uin){
                var searId = GameData.getUserSeatP(user.seatid);
                console.log("更新bi状态");
                self["user"+searId].active = true;
                self["user"+searId].thisJS.updateBiState(false);
                self.biState[searId] = null ;
            }
         }
       }
    },

    //点击比牌按钮
    biPaiClick:function(){
        console.log("biPaiClick");
         var info = GameData ;
         var losenum = 0 ;
         var allnum = 0 ;
         for(k in this.LoseUser){
            losenum = losenum + 1 ;
         }
         for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            //在玩牌的总数
            if(user.status>GameConfig.playerStatus.USER_STATE_READY){
                 allnum = allnum + 1 ;
            }
         }
         console.log("losenum--"+losenum);
         console.log("allnum--"+allnum);
         if(allnum - losenum == 2){
            for(var k in info.playerinfo){
                var user = info.playerinfo[k];
                if(user.uin != UserInfoModel.info.uin && this.LoseUser[user.uin] == null){
                     GameNet.sendRequest({cmd:DN_CMD.GoldFlowerUserCompareReq,
                        body:{
                            uin:UserInfoModel.info.uin,
                            desk_id:GameData.deskid,
                            compare_uin:user.uin,
                        }});
                }
            }
         }
         //比牌显示比牌选择
         for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            if(user.uin != UserInfoModel.info.uin && user.status>GameConfig.playerStatus.USER_STATE_READY){
                var searId = GameData.getUserSeatP(user.seatid);
                console.log("更新bi状态");
                this["user"+searId].active = true;
                this["user"+searId].thisJS.updateBiState(true);
                this.biState[searId] = 1 ;
            }
         }
    },
    /*
    {cmd:undefined,ret:undefined,sn:undefined,model:{
    deskid:undefined,roomid:undefined,op_uin:undefined,
    status:undefined,dealer:undefined,player_op_past_time:undefined,
    users:[{status:undefined,gold:undefined,chips:undefined,uin:undefined,nick:"舒服的慧慧",
    seatid:undefined,sex:undefined,round_chips:undefined,beauty:undefined,
    b_rank:undefined,decoration:undefined,vip_days:undefined,hiding:undefined,portrait:"IMG4",
    is_master:undefined},{status:undefined,gold:undefined,chips:undefined,uin:undefined,
    nick:"温驯的虹虹",seatid:undefined,sex:undefined,round_chips:undefined,beauty:undefined
    ,b_rank:undefined,decoration:undefined,vip_days:undefined,hiding:undefined,
    portrait:"IMG6",is_master:undefined}],next_uin:undefined,total_chips:undefined,play_info:{},
    dealer_seatid:undefined,beauty:undefined,gold:undefined,total_buyin_chips:undefined,buyin_limit_multi:undefined,safe_box_status:undefined,diamond:undefined,max_round:undefined,max_chips:undefined,base_chip:undefined,chip_list:[,,,,],can_operator:undefined,left_time:undefined}}
    */
    //玩家进入桌子,更新个人信息 
    updateHeadView:function(data){
        console.log("更新头像");
        dump(data.playerinfo)
        var info = GameData ;
        for(var i=0;i<GameData.PlayerNum;i++){
            this["user"+i].active = false;
        }

        for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            var searId = GameData.getUserSeatP(user.seatid);
            Log.logd(searId,"更新头像信息");
            this["user"+searId].active = true;
            this._players[user.uin] = this["user"+searId];
            this._players[user.uin].thisJS.initUserData(user);
        }
        //更新头像显示
        if(data.next_uin && data.next_uin>0)
        {
             var user =  GameData.getUserByUin(data.next_uin);
             var searId = GameData.getUserSeatP(user.seatid);
             if(data.player_op_past_time && data.player_op_past_time>0)
             {
                 this["user"+searId].active = true;
                 this["user"+searId].thisJS.startTimer(data.player_op_past_time);
             }
        }

    },
    UserReadyNtf:function(data){
        console.log("更新玩家准备状态");
        var info = GameData ;
         for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==data.uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                //this["user"+searId].active = true;
                this["user"+searId].thisJS.updateReadyStatus(true);
            }
         }
    },
    gameStart:function(info){
        Log.logd(GameData.PlayerNum,"headlayer start->");
        for(i=0;i<GameData.PlayerNum;i++){
            if(this["user"+i].active){
                this["user"+i].thisJS.hideStatus(false);
            }
        }
        this.scheduleOnce(function(){
            if(info.next_uin){
                if(UserInfoModel.info.uin==info.next_uin){
                     var user =  GameData.getUserByUin(info.next_uin);
                     var searId = GameData.getUserSeatP(user.seatid);
                     this["user"+searId].active = true;
                     this["user"+searId].thisJS.startTimer(10);
                }
            }
        },3);
    },

    //清桌
    clearTable:function(data){
      for(var j=0;j<GameData.PlayerNum;j++){
        if(this["user"+j].active){
        this["user"+j].thisJS.clearTable();
        }
      }
      this.LoseUser = {} ; 
    },

});
